package com.rcl.mvvmdemo.taobao

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.jetpacktaobao.R
import com.example.jetpacktaobao.adapter.OnSellListAdapter
import com.example.jetpacktaobao.base.LoadState
import com.example.jetpacktaobao.databinding.ActivityOnSellBinding
import com.example.jetpacktaobao.utils.SizeUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener

class OnSellActivity : AppCompatActivity() {

    private lateinit var binding: ActivityOnSellBinding

    private val mAdapter by lazy {
        OnSellListAdapter()
    }

    private val mViewModel by lazy {
        val factory = ViewModelProvider.NewInstanceFactory()
        ViewModelProvider(this, factory).get(OnSellViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityOnSellBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        initObserver()
    }

    //观察数据变化
    private fun initObserver() {
        mViewModel.apply {
            contentList.observe(this@OnSellActivity, Observer {
                //内容类列表更新
                //更新适配器
                mAdapter.setData(it)
            })

            loadState.observe(this@OnSellActivity, Observer {
                //根据加载的状态来更新ui的显示
                if (it != LoadState.LOADER_MORE_LOADING) {
                    //不是刷新Loading，才去隐藏，再根据对应的状态显示出来
                    hideAll()
                }
                when (it) {
                    LoadState.LOADING -> {
                        binding.loadingView.visibility = View.VISIBLE
                    }

                    LoadState.SUCCESS -> {
                        binding.refreshLayout.visibility = View.VISIBLE
                        binding.refreshLayout.finishLoadMore()
                    }

                    LoadState.EMPTY -> {
                        binding.emptyView.visibility = View.VISIBLE
                    }

                    LoadState.ERROR -> {
                        binding.errorView.visibility = View.VISIBLE
                    }

                    LoadState.LOADER_MORE_ERROR -> {
                        Toast.makeText(
                            this@OnSellActivity,
                            "网络不佳，请稍后重试！",
                            Toast.LENGTH_SHORT
                        ).show()
                        binding.refreshLayout.visibility = View.VISIBLE
                        binding.refreshLayout.finishLoadMore()
                    }

                    LoadState.LOADER_MORE_EMPTY -> {
                        Toast.makeText(this@OnSellActivity, "已经加载全部内容！", Toast.LENGTH_SHORT)
                            .show()
                        binding.refreshLayout.visibility = View.VISIBLE
                        binding.refreshLayout.finishLoadMore()
                    }

                    else -> {

                    }
                }
            })
        }.loadHomeArticleList()
    }

    //初始化数据
    private fun initView() {
        binding.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@OnSellActivity));
            setRefreshFooter(ClassicsFooter(this@OnSellActivity));
            setEnableAutoLoadMore(false)//是否启用列表惯性滑动到底部时自动加载更多
            setEnableRefresh(false)//是否启用下拉刷新功能
            setEnableOverScrollBounce(true)//是否启用越界回弹
            setOnLoadMoreListener {
                //去执行加载更多
                mViewModel.loadMore()
            }
        }
        binding.errorView.setOnClickListener {
            //重新加载数据
            mViewModel.loadHomeArticleList()
        }
        binding.contentListRv.run {
            layoutManager = LinearLayoutManager(this@OnSellActivity)
            adapter = mAdapter
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    outRect.run {
                        val padding = SizeUtils.dip2px(this@OnSellActivity, 4f)
                        top = padding
                        bottom = padding
                        left = padding
                        right = padding
                    }
                }
            })
        }
    }

    private fun hideAll() {
        binding.refreshLayout.visibility = View.GONE
        binding.errorView.visibility = View.GONE
        binding.emptyView.visibility = View.GONE
        binding.loadingView.visibility = View.GONE
    }
}